package com.amat0urheu.controller.studentController;

import com.alibaba.fastjson.JSONObject;
import com.amat0urheu.annotation.OperateLog;
import com.amat0urheu.pojo.Experiment;
import com.amat0urheu.pojo.ReturnRes;

import com.amat0urheu.pojo.race.Race;
import com.amat0urheu.service.RaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api")
public class RaceController {


    @Autowired
    RaceService raceService;

    @GetMapping("/admin/race/{pageSize}/{pageNum}")
    public ReturnRes addTeam(@PathVariable Integer pageSize,
                             @PathVariable Integer pageNum) {

        return raceService.getRaceView(pageSize, pageNum);
    }

    @GetMapping("/user/race/{pageSize}/{pageNum}")
    public ReturnRes getRaceForUser(@PathVariable Integer pageSize,
                             @PathVariable Integer pageNum) {

        return raceService.getRaceViewforUser(pageSize, pageNum);
    }
    @GetMapping("/user/getEndRace/{pageSize}/{pageNum}")
    public ReturnRes getEndRaceForUser(@PathVariable Integer pageSize,
                                    @PathVariable Integer pageNum) {

        return raceService.getEndRaceForUser(pageSize, pageNum);
    }

    @PostMapping("/admin/race")
    public ReturnRes operate(@RequestParam String action,
                             @RequestParam(name = "data") String raceJason) {
        if (action.equals("remove")) {
            Race race = JSONObject.parseObject(raceJason, Race.class);
        if ( raceService.removeRacebyId(race.getRid())!=0){

            return new ReturnRes(true, String.format("%s 删除成功", race.getRname()));
        } else {
            return new ReturnRes(false, String.format("%s 删除失败", race.getRname()));
        }

    }else {
            return new ReturnRes(false, "action error");
        }

    }
    @GetMapping("/user/race/{rid}")
    public ReturnRes getRaceInfoById(@PathVariable int rid) {

        return raceService.getRaceInfoById(rid);
    }

    @PostMapping("/user/participants/{pageSize}/{pageNum}")
    public ReturnRes getAllparticipants(@PathVariable Integer pageSize,
                             @PathVariable Integer pageNum,@RequestParam(name = "rid") Integer rid) {

        return   raceService.getAllparticipants(rid, pageSize, pageNum);
    }

    @PostMapping("/user/currentRaceRanking")
    public ReturnRes getCurrentRanking(@RequestParam(name = "rid") Integer rid) {

        return   raceService.getCurrentRanking(rid);
    }

    @OperateLog(description="1",action="提交flag")
    @PostMapping("/user/race/submitFlag")
    public ReturnRes submitRaceFlag(@RequestParam(name = "rid") Integer rid,@RequestParam(name = "flag") String flag) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        return   raceService.submitRaceFlag(authentication.getName(),rid,flag);
    }
    @OperateLog(description="0",action="添加了比赛")
    @PostMapping("/admin/race/addRace")
    public ReturnRes addRace(@RequestParam(name = "data") String raceJson,@RequestParam(name = "data2") Integer sceneId) {
        Race race = JSONObject.parseObject(raceJson, Race.class);

        return raceService.addRace(race,sceneId);
    }
    @PostMapping("/admin/race/updateRace")
    public ReturnRes updateRace(@RequestParam(name = "data") String raceJson,@RequestParam(name = "data2") Integer sceneId) {
        Race race = JSONObject.parseObject(raceJson, Race.class);

        return raceService.updateRace(race,sceneId);
    }
/*    @PostMapping("/admin/experiment")
    public ReturnRes operate(@RequestParam String action,
                             @RequestParam(name = "data") String experimentJson) {


        Experiment experiment= JSONObject.parseObject(experimentJson, Experiment.class);
        if (action.equals("save")) {
            experiment.setCreateTime(new Date());
            System.out.println(experiment);
            return  experimentService.saveExperiment(experiment);
        }else if(action.equals("update")){
            return experimentService.updateExperiment(experiment);
        } else {
            return new ReturnRes(false, "action value error");
        }
    }

     public ReturnRes updateRace(@RequestParam(name = "data") String raceJson,@RequestParam(name = "data2") Integer sceneId) {
        Race race = JSONObject.parseObject(raceJson, Race.class);
        System.out.println(raceJson+"  "+sceneId);
        return raceService.addRace(race,sceneId);
    }
    */


    @GetMapping("/admin/race/getRaceScene")
    public ReturnRes getRaceScenes() {
        return raceService.getRaceScenes();
    }

/*
    @PostMapping("/race/createRaceVm")
    public ReturnRes createRaceVm(@RequestParam(name = "data") String raceJson) {
        Race race = JSONObject.parseObject(raceJson, Race.class);

        return raceService.addRace(race);
    }
*/


    @GetMapping("/race/getAllRaceVms/{pageSize}/{pageNum}")
    public ReturnRes getAllRaceVms(@PathVariable int pageSize,
                                   @PathVariable int pageNum) {

        return raceService.getAllRaceVms(pageSize,pageNum);
    }


    //得到flag提交日志
    @GetMapping("/race/getSubmitFlagLogs/{pageSize}/{pageNum}")
    public ReturnRes getSubmitFlagLogs(@PathVariable int pageSize,
                                   @PathVariable int pageNum) {

        return raceService.getSubmitFlagLogs(pageSize,pageNum);
    }

    //获得队伍排名
    @PostMapping("/admin/race/getTeamRanking/{pageSize}/{pageNum}")
    public ReturnRes getTeamRanking(@PathVariable int pageSize,
                                       @PathVariable int pageNum,@RequestParam(name = "rid") Integer rid) {
        return raceService.getTeamRanking(pageSize,pageNum,rid);
    }

    //删除指定队伍的排名信息
    @PostMapping("/admin/race/removeRankingOfRace")
    public ReturnRes removeRankingOfRace(@RequestParam(name = "rid") String rid) {


        return raceService.removeRankingOfRace(rid);
    }
    @PostMapping("/user/race/getuserParticipated")
    public ReturnRes getuserParticipated(@RequestParam(name = "rid") Integer rid,Authentication authentication) {

        return raceService.getuserParticipated(rid,authentication.getName());
    }

    @PostMapping("/user/race/joinRace")
    @OperateLog(description="1",action="加入比赛")
    public ReturnRes joinRace(@RequestParam(name = "rid") Integer rid) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        return raceService.joinRace(rid,authentication.getName());
    }
    @PostMapping("/user/race/getRaceVmofTeam")
    public ReturnRes getRaceVmofTeam(@RequestParam(name = "rid")Integer rid,Authentication authentication) {

        return raceService.getRaceVmofTeam(authentication.getName(),rid);
    }
    @OperateLog(description="1",action="申请比赛资源")
    @PostMapping("/user/race/submitApplyRaceVm")
    public ReturnRes submitApplyRaceVm(@RequestParam(name = "rid")Integer rid) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return  raceService.submitApplyRaceVm(authentication.getName(),rid);
    }

    @OperateLog(description="1",action="释放比赛资源")
    @PostMapping("/user/race/releaseRaceVm")
    public ReturnRes releaseRaceVm(@RequestParam(name = "rid")Integer rid) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return  raceService.releaseRaceVm(authentication.getName(),rid);
    }
    @OperateLog(description="1",action="申请比赛客户资源")
    @PostMapping("/user/race/submitApplyRaceVmClient")
    public ReturnRes submitApplyRaceVmClient(@RequestParam(name = "rid")Integer rid) {
Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        return  raceService.submitApplyRaceVmClient(authentication.getName(),rid);
    }

@GetMapping("/admin/race/search/{pageSize}/{pageNum}")
public ReturnRes searchRaceByPage(@PathVariable int pageSize,
                             @PathVariable int pageNum,
                             @RequestParam String key,
                             @RequestParam String value) {
    return raceService.searchRaceByPage(key, value, pageNum, pageSize);
}
    @GetMapping("/admin/race/raceVm/search/{pageSize}/{pageNum}")
    public ReturnRes searchRaceVmByPage(@PathVariable int pageSize,
                                      @PathVariable int pageNum,
                                      @RequestParam String key,
                                      @RequestParam String value) {
        return raceService.searchRaceVm(key, value, pageNum, pageSize);
    }

    @GetMapping("/admin/race/raceFlag/search/{pageSize}/{pageNum}")
    public ReturnRes searchFlagLog(@PathVariable int pageSize,
                                        @PathVariable int pageNum,
                                        @RequestParam String key,
                                        @RequestParam String value) {
        return raceService.searchFlagLog(key, value, pageNum, pageSize);
    }

    @PostMapping("/user/getFlagofTeam/{pageSize}/{pageNum}")
    public ReturnRes getFlagofTeam(@PathVariable Integer pageSize,
                                   @PathVariable Integer pageNum,@RequestParam Integer rid) {

    return  raceService.getFlagofTeam(rid, pageSize, pageNum);
    }

    @GetMapping("/admin/getFlagofvM/{pageSize}/{pageNum}")
    public ReturnRes getFlagofvM(@PathVariable int pageSize,
                                   @PathVariable int pageNum) {

        return raceService.getFlagofvM(pageSize,pageNum);
    }

    @GetMapping("/admin/searchFlagofVm/{pageSize}/{pageNum}")
    public ReturnRes searchFlagofVm(@PathVariable int pageSize,
                                   @PathVariable int pageNum,
                                   @RequestParam String key,
                                   @RequestParam String value) {
        return raceService.searchFlagofVm(key, value, pageNum, pageSize);
    }

    @GetMapping("/admin/getRacevmCount")
    public ReturnRes getRacevmCount() {
        return raceService.getRacevmCount();
    }

    @GetMapping("/admin/getRaceInfo/{rid}")
    public ReturnRes getRaceInfo(@PathVariable int rid) {

        return raceService.getRaceInfo(rid);
    }


    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/experiment/releaseAllRaceVms")
    public ReturnRes releaseRaceVm() {
        return   raceService.releaseRaceAllVms();/*raceService.releaseRaceVm(authentication.getName(),rid)*/
    }

}